package com.treasure.mall.biz.repository.user;

import com.treasure.mall.biz.base.JpaSearchableRepository;
import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.biz.vo.user.UserTeamTreeVO;
import com.treasure.mall.zbq.common.ienum.user.UserEnums;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 *
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.biz.repository.user
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/6/11 10:31
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * Copyright 2019-2022 com.treasure.mall.biz.repository.user Inc. All rights reserved.
 */
@Repository
public interface UserRepository extends JpaSearchableRepository<UserPO, Long> {
    @Query("update UserPO po set po.fansCount=po.fansCount+?2 where po.fansCount+?2>=0 and po.userId=?1")
    @Modifying
    @Transactional
    void updateFansCount(Long userId, long count);

    /**
     * 根据登录token获取登录信息
     *
     * @param loginToken
     * @return
     */
    UserPO findFirstByLoginToken(String loginToken);


    /**
     * 根据用户名查用户信息
     *
     * @param username
     * @return
     */
    UserPO findByMobile(String username);

    /**
     * 获取用户列表
     *
     * @param idList ids
     * @return
     */
    @Query("select a from UserPO a where a.userId in ?1 and a.deleted=false")
    List<UserPO> getUserList(List<Long> idList);


    @Query("select a from UserPO a where a.userId in ?1 and a.userLevel= ?2 and a.deleted=false")
    List<UserPO> getUserList(List<Long> idList, UserEnums.UserLevel userLevel);


    /**
     * 获取我的下线
     *
     * @param userId
     * @param enabled
     * @return
     */
    @Query("select new com.treasure.mall.biz.vo.user.UserTeamTreeVO(user.userId,user.parentId,user.nickName) from UserPO user where user.parentId = ?1 and user.enabled=?2 and user.deleted=false")
    List<UserTeamTreeVO> getMyChildTree(Long userId, boolean enabled);


    /**
     * 获取用户的所有下线用户
     *
     * @param userId   用户id
     * @param userPath 用户path
     * @return 用户列表
     */
    @Query("select a from UserPO a where a.userId<>?1  and a.treePath like CONCAT(?2,'%')")
    List<UserPO> getTeamUserList(Long userId, String userPath);

    /**
     * 修改用户余额
     *
     * @param userId
     * @param balance
     */
    @Query("update UserPO user set user.toTalBalance=user.toTalBalance+?2 where user.userId=?1")
    @Modifying
    @Transactional
    void modifyBalance(Long userId, BigDecimal balance);


    /**
     * 修改用户锁定余额
     *
     * @param userId
     * @param balance
     */
    @Query("update UserPO user set user.lockBalance=user.lockBalance+?2 where user.userId=?1")
    @Modifying
    @Transactional
    void modifyLockBalance(Long userId, BigDecimal balance);

    @Query("update UserPO user set user.integral=user.integral+?2, user.version=user.version+1 " +
            "where user.userId=?1 and user.integral+?2>0 and user.version=?3")
    @Modifying
    @Transactional
    int modifyIntegral(Long userId, long integral, long version);


    @Query("update UserPO user set user.historyTotalAmount=user.historyTotalAmount+?2 where user.userId=?1")
    @Modifying
    @Transactional
    void modifyHistoryTotalAmount(Long userId, BigDecimal balance);

    /**
     * 修改用户待入账余额
     *
     * @param userId
     * @param balance
     */
    @Query("update UserPO user set user.balanceInFuture=user.balanceInFuture+?2 where user.userId=?1")
    @Modifying
    @Transactional
    void modifyBalanceInFuture(Long userId, BigDecimal balance);


    /**
     * 更新用户姓名
     *
     * @param userId
     * @param realName
     */
    @Query("update UserPO user set user.realName =?2 where user.userId in ?1")
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    void modifyUserRealName(Long userId, String realName);

    /**
     * 修改姓名
     *
     * @param userId
     * @param value
     */
    @Query("update UserPO user set user.realName=?2 where user.userId=?1")
    @Modifying
    @Transactional(rollbackFor = RuntimeException.class)
    void updateRealName(Long userId, String value);

    /**
     * 修改昵称
     *
     * @param userId
     * @param input
     */
    @Query("update UserPO user set user.nickName=?2 where user.userId=?1")
    @Modifying
    @Transactional(rollbackFor = RuntimeException.class)
    void modifyUserNick(Long userId, String input);

    /**
     * 修改头像
     *
     * @param userId
     * @param input
     */
    @Query("update UserPO user set user.avatar=?2 where user.userId=?1")
    @Modifying
    @Transactional(rollbackFor = RuntimeException.class)
    void modifyUserAvatar(Long userId, String input);


    /**
     * <p> 修改用户登录token </p>
     *
     * @param userId
     * @param loginToken
     * @return void
     * @throws
     * @author guomw (guomwchen@foxmail.com)
     * @date 2019/12/17 15:26
     * @since V1.0.0
     */
    @Query("update UserPO user set user.loginToken =?2 where user.userId in ?1")
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    void modifyUserLoginToken(Long userId, String loginToken);

    /**
     * 修改用户分享邀请二维码
     *
     * @param userId
     * @param inviteQrcode
     */
    @Query("update UserPO user set user.inviteQrcode =?2 where user.userId in ?1")
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    void modifyUserInviteQrcode(Long userId, String inviteQrcode);


    /**
     * 更改路径
     *
     * @param userId       用户id
     * @param path         路径
     * @param parentUserId 上级id
     */
    @Query("update UserPO user set user.treePath=?2,user.parentId=?3 where user.userId=?1")
    @Modifying
    @Transactional(rollbackFor = RuntimeException.class)
    void updatePath(Long userId, String path, Long parentUserId);

    /**
     * 更改路径
     *
     * @param userId 用户id
     * @param path   太阳路径
     */
    @Query("update UserPO user set user.treePath=?2 where user.userId=?1")
    @Modifying
    @Transactional(rollbackFor = RuntimeException.class)
    void updatePath(Long userId, String path);

    /**
     * 修改用户身份类型  普通用户和会员用户
     *
     * @param userId
     */
    @Query("update UserPO user set user.userType=?2,user.merchantPay=true,user.userLevel=?3 where user.userId=?1")
    @Modifying
    @Transactional(rollbackFor = RuntimeException.class)
    void modifyUserType(Long userId, UserEnums.UserType userType, UserEnums.UserLevel userLevel);


    @Query("update UserPO user set user.userLevel=?2 where user.userId=?1")
    @Modifying
    @Transactional(rollbackFor = RuntimeException.class)
    void modifyUserLevel(Long userId, UserEnums.UserLevel userLevel);


    /**
     * 修改用户有效支付订单总金额
     *
     * @param userId
     * @param money
     */
    @Query("update UserPO user set user.validPayOrderSum=user.validPayOrderSum+?2 where user.userId=?1")
    @Modifying
    @Transactional(rollbackFor = RuntimeException.class)
    void modifyValidPayOrderSum(Long userId, BigDecimal money);

    /**
     * 修改用户升级支付成功状态
     *
     * @param userId
     * @param merchantPay
     */
    @Query("update UserPO user set user.merchantPay=?2 where user.userId=?1")
    @Modifying
    @Transactional(rollbackFor = RuntimeException.class)
    void modifyMerchantPay(Long userId, Boolean merchantPay);


    /**
     * 商户审核通过后，需要会写商户ID
     *
     * @param userId
     * @param merchantId
     */
    @Query("update UserPO user set user.merchantId=?2 where user.userId=?1")
    @Modifying
    @Transactional(rollbackFor = RuntimeException.class)
    void modifyMerchantId(Long userId, Long merchantId);

    @Query("update UserPO user set user.validPayOrderSum=user.validPayOrderSum+?1 where user.userId=?2")
    @Modifying
    @Transactional
    void addOrderAmount(BigDecimal orderAmount, Long userId);


    /**
     * 根据条件获取用户列表
     *
     * @param userType
     * @param userLevel
     * @return
     */
    List<UserPO> findByUserTypeAndUserLevel(UserEnums.UserType userType, UserEnums.UserLevel userLevel);

    /**
     * 升级成达人，升级前先判断是否是会员
     *
     * @param userId
     * @param userLevel
     */
    @Query("update UserPO user set user.userLevel=?2 where user.userId=?1")
    @Modifying
    @Transactional
    void upgradeDrLevel(Long userId, UserEnums.UserLevel userLevel);

    @Query(value = "select ifnull(sum(user.integral),0) from zb_user user where user.integral>0", nativeQuery = true)
    Long integralHolding();

    @Query(value = "select ifnull(count(1),0) from zb_user where integral>0", nativeQuery = true)
    Long holdingUserCount();
}


